/*
 * ******************************************************************************
 *   Copyright (c) 2013-2014 Gabriele Mariotti.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *  *****************************************************************************
 */

package it.gmariotti.cardslib.demo.fragment.v1;

import it.gmariotti.cardslib.demo.ResourceTable;
import it.gmariotti.cardslib.demo.cards.CustomCard;
import it.gmariotti.cardslib.library.Constants;
import it.gmariotti.cardslib.library.internal.*;
import it.gmariotti.cardslib.library.utils.PixelMapUtils;
import it.gmariotti.cardslib.library.view.CardView;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;

/**
 * Card Expand Examples
 *
 * @author Gabriele Mariotti (gabri.mariotti@gmail.com)
 */
public class CardExpandFragment extends MaterialV1Fragment {


    ScrollView mScrollView;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_demo_fragment_card_expand);

        mScrollView = (ScrollView) findComponentById(ResourceTable.Id_card_scrollview);
        initCards();
    }

    @Override
    protected int getSubTitleHeaderResourceId() {
        return ResourceTable.String_header_title_cardexpandsubtitle;
    }

    @Override
    protected int getTitleHeaderResourceId() {
        return ResourceTable.String_header_title_group1;
    }

    @Override
    protected String getDocUrl() {
        return "";
    }

    @Override
    protected String getSourceUrl() {
        return "";
    }


    private void initCards() {
        init_standard_header_with_expandcollapse_button();
        init_custom_card_expand();
        init_custom_card_expand_clicking_text();
        init_custom_card_expand_clicking_image();
        init_custom_card_expand_inside();
        init_custom_card_expand_programmatic();
    }


    /**
     * This method builds a standard header with base expand/collapse
     */
    private void init_standard_header_with_expandcollapse_button() {

        //Create a Card
        Card card = new Card(this);

        //Create a CardHeader
        CardHeader header = new CardHeader(this);

        //Set the header title
        try {
            header.setTitle(getResourceManager().getElement(ResourceTable.String_demo_header_basetitle).getString());
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }

        //Set visible the expand/collapse button
        header.setButtonExpandVisible(true);

        //Add Header to card
        card.addCardHeader(header);

        //This provides a simple (and useless) expand area
        CardExpand expand = new CardExpand(this);
        //Set inner title in Expand Area
        try {
            expand.setTitle(getResourceManager().getElement(ResourceTable.String_demo_expand_customtitle3).getString());
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }
        card.addCardExpand(expand);

        //Set card in the cardView
        CardView cardView = (CardView) this.findComponentById(ResourceTable.Id_carddemo_example_card_expand1);
        cardView.setCard(card);
    }

    /**
     * This method builds a custom card with expand/collapse action clickable in all card view
     */
    private void init_custom_card_expand() {

        //Create a Card
        CustomCard card = new CustomCard(this);

        //This provides a simple (and useless) expand area
        CardExpand expand = new CardExpand(this);
        //Set inner title in Expand Area
        try {
            expand.setTitle(getResourceManager().getElement(ResourceTable.String_demo_expand_customtitle3).getString());
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }
        card.addCardExpand(expand);


        //Set card in the cardView
        CardView cardView = (CardView) this.findComponentById(ResourceTable.Id_carddemo_example_card_expand2);
        ViewToClickToExpand viewToClickToExpand =
                ViewToClickToExpand.builder()
                        .setupView(cardView);
        card.setViewToClickToExpand(viewToClickToExpand);
        cardView.setCard(card);
    }


    /**
     * This method builds a custom card with expand/collapse action clickable in all card view
     */
    private void init_custom_card_expand_clicking_text() {

        //Create a Card
        CustomCard2 card = new CustomCard2(this);
        card.setTitle("Click here to expand/collapse");

        //This provides a simple (and useless) expand area
        CardExpand expand = new CardExpand(this);
        //Set inner title in Expand Area
        try {
            expand.setTitle(getResourceManager().getElement(ResourceTable.String_demo_expand_customtitle3).getString());
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }
        card.addCardExpand(expand);


        //Set card in the cardView
        CardView cardView = (CardView) this.findComponentById(ResourceTable.Id_carddemo_example_card_expand3);
        cardView.setCard(card);
    }

    /**
     * This method builds a custom card with expand/collapse action clickable in all card view
     */
    private void init_custom_card_expand_clicking_image() {

        //Create a Card
        Card card = new Card(this);

        //This provides a simple (and useless) expand area
        CardExpand expand = new CardExpand(this);
        //Set inner title in Expand Area
        try {
            expand.setTitle(getResourceManager().getElement(ResourceTable.String_demo_expand_customtitle3).getString());
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }
        card.addCardExpand(expand);

        CustomThumbnail thumb = new CustomThumbnail(this);
        thumb.setDrawableResource(ResourceTable.Media_ic_smile);
        card.addCardThumbnail(thumb);

        //Set card in the cardView
        CardView cardView = (CardView) this.findComponentById(ResourceTable.Id_carddemo_example_card_expand4);
        cardView.setCard(card);
    }

    /**
     * This method builds a card with a collpse/expand section inside
     */
    private void init_custom_card_expand_inside() {

        //Create a Card
        Card card = new Card(this);

        //Create a CardHeader
        CardHeader header = new CardHeader(this);

        //Set the header title
        try {
            header.setTitle(getResourceManager().getElement(ResourceTable.String_demo_header_expand_area_inside).getString());
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }
        //Add Header to card
        card.addCardHeader(header);

        //This provides a simple (and useless) expand area
        CardExpandInside expand = new CardExpandInside(this);
        card.addCardExpand(expand);

        //Set card in the cardView
        CardView cardView = (CardView) this.findComponentById(ResourceTable.Id_carddemo_example_card_expand5);
        ViewToClickToExpand viewToClickToExpand =
                ViewToClickToExpand.builder()
                        .highlightView(false)
                        .setupView(cardView);
        card.setViewToClickToExpand(viewToClickToExpand);

        card.setOnExpandAnimatorEndListener(new Card.OnExpandAnimatorEndListener() {
            @Override
            public void onExpandEnd(Card card) {
                /*
                if (mScrollView!=null){
                    mScrollView.post(new Runnable() {
                        public void run() {
                            mScrollView.scrollTo(0, mScrollView.getBottom());
                        }
                    });
                }*/
            }
        });

        cardView.setCard(card);
    }

    /**
     * This method builds a card with a collpse/expand action in programmatic way
     */
    private void init_custom_card_expand_programmatic() {

        //Create a Card
        final Card card = new Card(this);

        //This provides a simple (and useless) expand area
        CardExpand expand = new CardExpand(this);
        //Set inner title in Expand Area
        try {
            expand.setTitle(getResourceManager().getElement(ResourceTable.String_demo_expand_customtitle3).getString());
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }
        card.addCardExpand(expand);

        ViewToClickToExpand viewToClickToExpand = ViewToClickToExpand.builder().enableForExpandAction();
        card.setViewToClickToExpand(viewToClickToExpand);

        Text tx = (Text) this.findComponentById(ResourceTable.Id_carddemo_example_card_expand6_text);
        tx.setClickedListener(v -> card.doToogleExpand());

        //Set card in the cardView
        CardView cardView = (CardView) this.findComponentById(ResourceTable.Id_carddemo_example_card_expand6);
        cardView.setCard(card);
    }


    class CustomCard2 extends Card {

        public CustomCard2(Context context) {
            super(context, ResourceTable.Layout_carddemo_example_cardexpand_inner_content);
        }

        @Override
        public void setupInnerViewElements(ComponentContainer parent, Component view) {

            if (view != null) {
                Text mTitleView = (Text) view.findComponentById(it.gmariotti.cardslib.library.ResourceTable.Id_card_main_inner_simple_title);
                if (mTitleView != null) {
                    mTitleView.setText(mTitle);

                    ViewToClickToExpand viewToClickToExpand =
                            ViewToClickToExpand.builder()
                                    .setupView(mTitleView);
                    setViewToClickToExpand(viewToClickToExpand);
                }
            }
        }
    }


    class CustomThumbnail extends CardThumbnail {

        public CustomThumbnail(Context context) {
            super(context);
        }

        @Override
        public void setupInnerViewElements(ComponentContainer parent, Component imageView) {

            ViewToClickToExpand viewToClickToExpand =
                    ViewToClickToExpand.builder()
                            .highlightView(false)
                            .setupView(imageView);
            getParentCard().setViewToClickToExpand(viewToClickToExpand);
        }
    }

    class CardExpandInside extends CardExpand {

        public CardExpandInside(Context context) {
            super(context, ResourceTable.Layout_carddemo_example_expandinside_expand_layout);
        }

        @Override
        public void setupInnerViewElements(ComponentContainer parent, Component view) {

            Image img = (Image) view.findComponentById(ResourceTable.Id_carddemo_inside_image);

            //It is just an example. You should load your images in an async way
            if (img != null) {
                try {
                    img.setImageElement(PixelMapUtils.prepareElementById(getContext(), ResourceTable.Media_rose));
                } catch (Exception e) {
                    HiLog.error(Constants.LABEL_LOG, e.toString());
                }
            }
        }

    }
}
